package com.anche.cywebnew.Controller.systemManager;

import com.anche.cywebnew.Pojo.systemManager.SysRole;
import com.anche.cywebnew.Service.systemManager.RoleService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/system/role")
@Controller
@Validated
public class RoleController {

    @Autowired
    private RoleService roleService;

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);


    @RequestMapping("/manager")
    public String toOrganPage() {
        return "/systemManager/role";
    }

    @RequestMapping("/rolePager")
    @ResponseBody
    public Object getRolePager(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<SysRole> sysRoles = roleService.getRolePager(page, limit);
            logger.info("<查询角色列表成功>");
            map.put("code", "0");
            map.put("count", sysRoles.getTotal());
            map.put("msg", "查询成功");
            map.put("data", sysRoles);
        } catch (Exception e) {
            logger.error("<查询角色列表异常>:{}", e);
            e.printStackTrace();
            map.put("code", "1");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PutMapping("/status")
    @ResponseBody
    @OptLog(title = "更新角色状态信息", type = OptType.UPDATE)
    public Object setRoleStatus(@RequestParam("id") String id, @RequestParam("status") String status) {
        try {
            roleService.setRoleStatus(id, status);
            logger.info("<更新id为[{}]的角色状态成功>", id);
            return ResultUtil.success("状态更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新id为[{}]的角色状态异常>:{}", id, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @PostMapping(value = "/roleInfo")
    @ResponseBody
    @OptLog(title = "添加角色信息", type = OptType.ADD)
    public Object addRole(SysRole sysRole) {
        try {
            roleService.addRole(sysRole);
            logger.info("<新增id为[{}]的角色成功>", sysRole.getId());
            return ResultUtil.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<新增id为[{}]的角色异常>:{}", sysRole.getId(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @PutMapping(value = "/roleInfo")
    @ResponseBody
    @OptLog(title = "更新角色信息", type = OptType.UPDATE)
    public Object setRole(SysRole sysRole) {
        try {
            roleService.setRole(sysRole);
            logger.info("<更新id为[{}]的角色成功>", sysRole.getId());
            return ResultUtil.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新id为[{}]的角色异常>:{}", sysRole.getId(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @DeleteMapping("/roleInfo")
    @ResponseBody
    @OptLog(title = "删除角色信息", type = OptType.DEL)
    public Object delRole(@RequestParam("id") String id) {
        try {
            roleService.delRole(id);
            logger.info("<删除id为[{}]的角色成功>", id);
            return ResultUtil.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<删除id为[{}]的角色异常>:{}", id, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping("/roleList")
    @ResponseBody
    public Object getRoleList(){
        try {
            List<SysRole> sysRoles = roleService.getRoleList();
            logger.error("<查询角色列表正常>");
            return ResultUtil.success(sysRoles);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("<查询角色列表异常>:{}", e);
            return ResultUtil.error(0, e.getMessage());
        }
    }
}
